package com.miniapp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.miniapp.dto.PageDTO;
import com.miniapp.dto.SimpleSearchDTO;
import com.miniapp.dto.form.MiniAppCreateDTO;
import com.miniapp.entity.*;
import com.miniapp.entity.enums.UserType;
import com.miniapp.service.MiniProductPriceService;
import com.miniapp.service.MiniProductService;
import com.miniapp.service.MiniUserProductService;
import com.miniapp.service.MiniUserService;
import com.miniapp.utils.R;
import com.miniapp.validator.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api
@RestController()
@RequestMapping("/miniapp")
public class MiniIndexController extends AbstractController {

    @Resource
    private MiniProductService miniProductService;
    @Resource
    private MiniUserProductService miniUserProductService;
    @Resource
    private MiniProductPriceService miniProducPriceService;
    @Resource
    private MiniUserService miniUserService;

    /**
     * 小程序类型-->>获取用户产品列表---分页
     */
    @ApiOperation(value = "分页获取用户产品列表" ,notes = "分页获取用户产品列表")
    @GetMapping("/list")
    public R list(){
        SimpleSearchDTO simpleSearchDTO = getSearchDtoFromJqGrid();
        MiniUserEntity user = miniUserService.getById(this.getUserId());
        if (user.getUserType().equals(UserType.COMMON.getValue())){
            simpleSearchDTO.addFiled("buy_user_id","eq",this.getUserId());
        }else if (user.getUserType().equals(UserType.AGENCY.getValue())){
        }
        simpleSearchDTO.addSort("create_time",false);
        simpleSearchDTO.setPageSize(9L);
        PageDTO<MiniUserProductEntity> page = miniUserProductService.findPageBySql(simpleSearchDTO);
        return R.ok().put("page", page);
    }
    /**
     * 小程序类型-->>获取用户产品列表  -->不分页
     */
    @ApiOperation(value = "商户助手使用，不分页获取用户产品列表" ,notes = "商户助手使用，不分页获取用户产品列表 ")
    @GetMapping("product/list")
    public R nopage(){
        SimpleSearchDTO simpleSearchDTO = getSearchDtoFromJqGrid();
        MiniUserEntity user = miniUserService.getById(this.getUserId());
        if (user.getUserType().equals(UserType.COMMON.getValue())){
            simpleSearchDTO.addFiled("buy_user_id","eq",this.getUserId());
        }else if (user.getUserType().equals(UserType.AGENCY.getValue())){
        }
        simpleSearchDTO.addSort("create_time",false);
        List<MiniUserProductEntity> list = miniUserProductService.findAllBySimpleSearch(simpleSearchDTO);
        if (list.size()>0){
            return R.ok().put("list", list).put("default",list.get(0));
        }
        return R.ok().put("list", list);
    }

    /**
     * 小程序类型-->产品种类
     */
    @ApiOperation(value = "产品种类" ,notes = "产品种类")
    @GetMapping("/type")
    public R type(){
        List<MiniProductEntity> results = miniProductService.findAll();
        return R.ok().put("items", results);
    }

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("/user")
    public R user(){

        String [] props = {"id,name,mobile,user_type"};
        MiniUserEntity user = miniUserService.getOne(new QueryWrapper<MiniUserEntity>(new MiniUserEntity(),props));
        return R.ok().put("user", user);
    }

    /**
     * 通过产品ID获取产品价格列表
     */
    @ApiOperation(value = "通过产品ID获取产品价格列表" ,notes = "通过产品ID获取产品价格列表")
    @PostMapping("/create")
    public R createMiniApp(@RequestBody MiniAppCreateDTO miniAppCreateDTO, HttpServletRequest request){

        ValidatorUtils.validateEntity(miniAppCreateDTO);
        return  miniUserProductService.createMiniapp(miniAppCreateDTO,this.getUserId(),request);

    }

    /**
     * 通过产品ID获取产品价格列表
     */
    @ApiOperation(value = "通过产品ID获取产品价格列表" ,notes = "通过产品ID获取产品价格列表")
    @GetMapping("/getpricebyid/{id}")
    public R getPriceById(@PathVariable("id") Long id){
        List<MiniProductPriceEntity> miniProductPrices  = miniProducPriceService.findByProductId(id);
        for (MiniProductPriceEntity miniProductPriceEntity:miniProductPrices){
            miniProductPriceEntity.setMiniProduct(miniProductService.getById(miniProductPriceEntity.getProductId()));
        }
        return R.ok().put("items",miniProductPrices);
    }

    /**
     * 获取产品接入状态
     */
    @GetMapping("/get_status")
    public R getStatus( @RequestParam(value = "appid", required = true) String appid ){

        MiniUserProductEntity miniUserProduct = miniUserProductService.findByAppid(appid);

        return R.ok().put("status",miniUserProduct.getStatus());
    }

}
